package com.lxq.market.util;

import com.lxq.market.annotation.ModelField;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class ResultSetConvert {

    private static ResultSetConvert resultSetConvert;

    private ResultSetConvert() {
    }

    public static ResultSetConvert getInstance() {
        if (resultSetConvert == null) resultSetConvert = new ResultSetConvert();
        return resultSetConvert;
    }

    public <T> List<T> convert(ResultSet resultSet, Class<T> t) {
        HashMap<String, Field> findFields = new HashMap<>();
        ArrayList<T> resultList;
        Field[] declaredFields = t.getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(false);
            ModelField modelField = field.getAnnotation(ModelField.class);
            if (modelField != null) {
                findFields.put(modelField.name(), field);
            }
        }
        if (resultSet!=null){
            resultList = new ArrayList<>();
        }else{
            return null;
        }
        while (true) {
            try {
                if (!resultSet.next()) break;
                T newLine = t.getDeclaredConstructor().newInstance();
                findFields.forEach((annoKey, field) -> {
                    try {
                        String fieldTypeName = field.getType().getName();
                        field.setAccessible(true);
                        switch (fieldTypeName) {
                            case "java.lang.String":
                                String value = resultSet.getString(annoKey);
                                field.set(newLine, value);
                                break;
                            case "int":
                                int intValue = resultSet.getInt(annoKey);
                                field.set(newLine, intValue);
                                break;
                            case "double":
                                double doubleValue = resultSet.getDouble(annoKey);
                                field.set(newLine, doubleValue);
                                break;
                            case "java.util.Date":
                                Date dateValue = resultSet.getDate(annoKey, Calendar.getInstance(Locale.SIMPLIFIED_CHINESE));
                                field.set(newLine, dateValue);
                                break;
                        }
                    } catch (Exception sqlException) {
                        sqlException.printStackTrace();
                    }
                });
                resultList.add(newLine);
            } catch (Exception throwables) {
                throwables.printStackTrace();
            }
        }
        return resultList;
    }

    public <T> T convertOne(ResultSet resultSet, Class<T> t) {
        List<T> list = convert(resultSet, t);
        if (list.size()==0) {
            return null;
        }
        return list.get(0);
    }
}
